﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class HasharForm : Form
    {
        public HasharForm()
        {
            InitializeComponent();
        }

        int regID, avpID;
        tb_hashar hashar;

        private void HasharForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                toolStrip1.Visible = false;
            }
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            regID = Convert.ToInt32(cbRegion.SelectedValue);
            cbAVP.Enabled = true;
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                numPeopleCount.Enabled = dtpDate.Enabled = tbInfo.Enabled = numDaysCount.Enabled = true;
                dtpDate.Value = DateTime.Now;
                tbInfo.Text = string.Empty;
                numPeopleCount.Value = 0;
                ShowHashars();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (numPeopleCount.Value == 0 || numDaysCount.Value == 0)
            {
                if(InformationClass.Lang)
                MessageBox.Show("Количество дней и человек в хашаре должно быть больше 0", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show("Хашар кунлари ва унда иштирок этган кишилар сони 0 дан кўп бўлиши керак", "Хато", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (hashar == null)
            {
                hashar = new tb_hashar
                {
                    AVPID = avpID,
                    Date = dtpDate.Value.Date,
                    DaysCount = (int)numDaysCount.Value,
                    Details = tbInfo.Text,
                    PeopleCount = (int)numPeopleCount.Value
                };
                DBClass.Hashars.InsertOnSubmit(hashar);
            }
            else
            {
                hashar.PeopleCount = (int)numPeopleCount.Value;
                hashar.Details = tbInfo.Text;
                hashar.DaysCount = (int)numDaysCount.Value;
            }
            try
            {

                DBClass.RESPContext.SubmitChanges();

                string msg = InformationClass.Lang ? " Добавить еще запись?" : "Яна ёзув қўшиладими?";

                if (MessageBox.Show(msgClass.msgSubmitChanges + msg, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.No)
                    Close();
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                DBClass.Hashars.DeleteOnSubmit(hashar);
                DBClass.RESPContext.SubmitChanges();
            }

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void showHasharsButton_Click(object sender, EventArgs e)
        {
            showHasharsForm shForm = new showHasharsForm();
            shForm.ShowDialog();
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            if (avpID > 0)
                ShowHashars();
        }

        private void ShowHashars()
        {
            hashar = DBClass.Hashars.Where(wh => wh.AVPID == avpID && wh.Date == dtpDate.Value.Date).SingleOrDefault();
            if (hashar != null)
            {
                tbInfo.Text = hashar.Details;
                numDaysCount.Value = (decimal)hashar.DaysCount;
                numPeopleCount.Value = (decimal)hashar.PeopleCount;
            }
            else
            {
                tbInfo.Text = string.Empty;
                numPeopleCount.Value = numDaysCount.Value = 0;
            }
        }
    }
}
